﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;


public class GameStateManager  {

    private Dictionary<GameStateType, IGameState> gameStates;
    private IGameState currentState;

    public GameStateManager()
    {
        gameStates = new Dictionary<GameStateType, IGameState>();

        IGameState gameState;

        gameState = new LoginState();
        gameStates.Add(gameState.GetStateType(), gameState);
        //gameState = new RoleState();
        //gameStates.Add(gameState.GetStateType(), gameState);
        //gameState = new PlayState();
        //gameStates.Add(gameState.GetStateType(), gameState);
    }

    // 获取当前状态
    public IGameState GetCurState()
    {
        return currentState;
    }
    //改变状态
    public void ChangeGameStateTo(GameStateType stateType)
    {
        if (currentState != null && currentState.GetStateType() != GameStateType.GST_Loading && currentState.GetStateType() == stateType) return;

        if (gameStates.ContainsKey(stateType))
        {
            if (currentState != null)
            {
                currentState.Exit();
            }

            currentState = gameStates[stateType];
            currentState.Enter();
        }
    }
    //进入默认状态
    public void EnterDefaultState()
    {
        ChangeGameStateTo(GameStateType.GST_Login);
    }

    public void FixedUpdate(float fixedDeltaTime)
    {
        if (currentState != null)
        {
            currentState.FixedUpdate(fixedDeltaTime);
        }
    }
    //更新
    public void Update(float fDeltaTime)
    {
        GameStateType nextStateType = GameStateType.GST_Continue;
        if (currentState != null)
        {
            nextStateType = currentState.Update(fDeltaTime);
        }

        if (nextStateType > GameStateType.GST_Continue)
        {
            ChangeGameStateTo(nextStateType);
        }
    }
    //获取状态
    public IGameState getState(GameStateType type)
    {
        if (!gameStates.ContainsKey(type))
        {
            return null;
        }
        return gameStates[type];
    }
}

